package com.ocom.aiot.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.ocom.aiot.dao.IDaoMjAppVerInfo;
import com.ocom.aiot.service.MjAppVerInfoService;
import com.ocom.common.entity.aiot.EntityMjAppVerInfo;
import com.ocom.common.enums.DataSourceEnum;
import com.ocom.common.enums.MasterSlaveEnum;
import com.ocom.common.utils.StringUtils;
import com.ocom.aiot.vo.accessdevice.GetDeviceVerInfoVo;
import com.ocom.aiot.vo.accessdevice.AddDeviceVerInfoVo;
import com.ocom.mysql.aspect.DataSourceAspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class MjAppVerInfoServiceImpl extends ServiceImpl<IDaoMjAppVerInfo, EntityMjAppVerInfo> implements MjAppVerInfoService {



    @Autowired
    private IDaoMjAppVerInfo daoMjAppVerInfo;
    /**
     * 获取升级文件记录(getMjOsVerInfo)
     *
     * @param getDeviceVerInfoVo
     * @return */
    @Override
    public List<EntityMjAppVerInfo> AccessDeviceGetDeviceVerInfo(GetDeviceVerInfoVo getDeviceVerInfoVo) {
        QueryWrapper<EntityMjAppVerInfo> query = Wrappers.query();
        if (StringUtils.isNotEmpty(getDeviceVerInfoVo.getAppVer())){
            query.eq("app_ver", getDeviceVerInfoVo.getAppVer());
        }
        if (StringUtils.isNotNull(getDeviceVerInfoVo.getApkType())){
            query.eq("apk_type", getDeviceVerInfoVo.getApkType());
        }
        if (StringUtils.isNotEmpty(getDeviceVerInfoVo.getOldAppVer())){
            query.eq("old_app_ver", getDeviceVerInfoVo.getOldAppVer());
        }
        if (StringUtils.isNotNull(getDeviceVerInfoVo.getStatus())){
            query.eq("status", getDeviceVerInfoVo.getStatus());
        }
        if (StringUtils.isNotNull(getDeviceVerInfoVo.getId())){
            query.eq("id", getDeviceVerInfoVo.getId());
        }
        PageHelper.startPage(getDeviceVerInfoVo.getPage(), getDeviceVerInfoVo.getSize());

        return daoMjAppVerInfo.selectList(query);
    }


    /**
     * 保存升级文件记录(addMjOsVerInfo)*/
    @Override
    public void AccessDeviceAddDeviceVerInfo(AddDeviceVerInfoVo addDeviceVerInfoVo) {

        EntityMjAppVerInfo entityMjAppVerInfo = new EntityMjAppVerInfo();
        entityMjAppVerInfo.setAppVer(addDeviceVerInfoVo.getAppVer());
        entityMjAppVerInfo.setOldAppVer(addDeviceVerInfoVo.getOldAppVer());
        entityMjAppVerInfo.setApkFileUrl(addDeviceVerInfoVo.getApkFileUrl());
        entityMjAppVerInfo.setApkType(addDeviceVerInfoVo.getApkType());
        entityMjAppVerInfo.setStatus(addDeviceVerInfoVo.getStatus());
        daoMjAppVerInfo.insert(entityMjAppVerInfo);
    }

    //获取固件版本信息
    @Override
    public EntityMjAppVerInfo GetMjAppVer(String appVer) {
        QueryWrapper<EntityMjAppVerInfo> query = Wrappers.query();
        query.eq("app_ver",appVer);
        return daoMjAppVerInfo.selectOne(query);
    }

    //2.7.3.删除设备升级版本（deleteDeviceVerInfo）
    @Override
    public void AccessDeviceDeleteDeviceVerInfo(Long id) {
        daoMjAppVerInfo.deleteById(id);
    }


    //从门禁设备app版本信息表mj_app_ver_info查找apk_type=2、app_ver=mj_device_info . app_ver_tab的记录
    @DataSourceAspect(type= DataSourceEnum.MAIN_DB,isMaster = MasterSlaveEnum.SLAVE)
    @Override
    public EntityMjAppVerInfo selectSinglechipAppVerInfo(String appVerTab) {
        QueryWrapper<EntityMjAppVerInfo> query = Wrappers.query();
        query.eq("apk_type",2).eq("app_ver",appVerTab).last("limit 1");
        return this.getOne(query);
    }


}
